﻿using Infrastructures;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConcreteService
{
    public class AppConfig
    {
        private static IList<School> _lstSchool = null;
        private static IList<BaseData> _lstBaseData = null;
        private static IList<Area> _lstArea = null;
        private static IList<SpliteWord> _lstSpliteWord = null;
        private static IDictionary<APPRole, IList<FieldName>> _lstFields = null;

        public static void RefreshBaseData()
        {
            UtilService service = ServiceFactory.CreateInstance<UtilService>();
            _lstBaseData = service.ListBaseData();
            _lstArea = service.ListArea();
            _lstSpliteWord = service.ListSpliteWord();
            _lstSchool = service.ListSchool();
            _lstFields = ListFieldByRole();
        }

        public static IList<BaseData> ListRuleByFun(BaseDataFun fun)
        {
            if (_lstBaseData == null || _lstBaseData.Count() == 0)
            {
                RefreshBaseData();
            }
            return _lstBaseData.Where(x => x.Fun == fun).ToList();
        }

        public static IList<BaseData> ListRuleByFun(BaseDataFun fun, int parentno)
        {
            if (_lstBaseData == null || _lstBaseData.Count() == 0)
            {
                RefreshBaseData();
            }
            return _lstBaseData.Where(x => x.Fun == fun && x.ParentNo == parentno).ToList();
        }

        public static BaseData GetBaseData(BaseDataFun fun)
        {
            if (_lstBaseData == null || _lstBaseData.Count() == 0)
            {
                RefreshBaseData();
            }
            return _lstBaseData.Where(x => x.Fun == fun).FirstOrDefault();

        }
        public static BaseData GetBaseData(BaseDataFun fun, int value)
        {
            if (_lstBaseData == null || _lstBaseData.Count() == 0)
            {
                RefreshBaseData();
            }
            return _lstBaseData.Where(x => x.Fun == fun && x.ValueFrom.ToInt() <= value && x.ValueTo.ToInt() >= value).FirstOrDefault();
        }
        public static BaseData GetBaseData(int ruleNO)
        {
            if (_lstBaseData == null || _lstBaseData.Count() == 0)
            {
                RefreshBaseData();
            }
            return _lstBaseData.Where(x => x.RuleNO == ruleNO).FirstOrDefault();
        }

        public static IList<School> ListSchool(int cityCode)
        {
            if (_lstSchool == null || _lstSchool.Count() == 0)
            {
                RefreshBaseData();
            }
            return _lstSchool.Where(x => x.CityNO == cityCode).ToList();
        }

        public static IList<School> ListSchool()
        {
            return _lstSchool;
        }

        public static School GetSchool(int schoolNO)
        {
            if (_lstSchool == null || _lstSchool.Count() == 0)
            {
                RefreshBaseData();
            }
            return _lstSchool.Where(x => x.SchoolNO == schoolNO).FirstOrDefault();
        }

        public static string GetDefauleAssocGrade()
        {
            var data = GetBaseData(BaseDataFun.StudentLevel, 1);
            return data.RuleNO.ToString();
        }

        public static SpliteWord GetSpliteWord(int id)
        {
            if (_lstSpliteWord == null || _lstSpliteWord.Count() == 0)
            {
                RefreshBaseData();
            }
            return _lstSpliteWord.Where(x => x.ID == id).FirstOrDefault();
        }

        public static Area GetArea(int id)
        {
            if (_lstArea == null || _lstArea.Count() == 0)
            {
                RefreshBaseData();
            }
            return _lstArea.Where(x => x.Code == id).FirstOrDefault();
        }

        public static string GetAreaName(int id)
        {
            if (_lstArea == null || _lstArea.Count() == 0)
            {
                RefreshBaseData();
            }
            Area area = _lstArea.Where(x => x.Code == id).FirstOrDefault();
            return area == null ? "" : area.Name;
        }

        public static IList<Area> ListArea(AreaFun fun)
        {
            if (_lstArea == null || _lstArea.Count() == 0)
            {
                RefreshBaseData();
            }
            return _lstArea.Where(x => x.Fun == fun).ToList<Area>();
        }

        public static IList<FieldName> ListFieldByRole(APPRole role)
        {
            if (!_lstFields.ContainsKey(role))
                return new List<FieldName>();

            return _lstFields.FirstOrDefault(x => x.Key == role).Value;
        }

        private static IDictionary<APPRole, IList<FieldName>> ListFieldByRole()
        {
            IDictionary<APPRole, IList<FieldName>> dic = new Dictionary<APPRole, IList<FieldName>>();

            IList<FieldName> lstStudent = new List<FieldName>() { FieldName.Area, FieldName.AreaDetails, FieldName.ActivityType, FieldName.StudentInterval, FieldName.ActivityNum, 
                FieldName.Introduction, FieldName.Supervision, FieldName.ContactName, FieldName.ContactPhone, FieldName.Product,
                FieldName.Poster, FieldName.Booth, FieldName.KTBoard, FieldName.Banner, FieldName.Microphone, FieldName.Rollup, 
                FieldName.Leaflet, FieldName.Tent, FieldName.Sound, FieldName.Desk, FieldName.Other, FieldName.Photos, FieldName.Video, FieldName.Tweets, FieldName.PublicizeType, FieldName.ExecutiveTeam };

            dic.Add(APPRole.Student, lstStudent);
            dic.Add(APPRole.Company, lstStudent);

            return dic;
        }
    }

    public class ServiceFactory
    {
        public static T CreateInstance<T>()
        {
            return FactoryCreator.Create<T>();
        }
    }
}
